package cn.tristenstudy.blog.controller.admin;

import cn.tristenstudy.blog.pojo.Blog;
import cn.tristenstudy.blog.pojo.Tag;
import cn.tristenstudy.blog.pojo.Type;
import cn.tristenstudy.blog.pojo.vo.BlogQuery;
import cn.tristenstudy.blog.service.BlogService;
import cn.tristenstudy.blog.service.TagService;
import cn.tristenstudy.blog.service.TypeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author xichen1.li
 * @date 2020-12-23 23:21
 */
@Controller
@RequestMapping("/admin/blog")
public class BlogManagerController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    /**
     * 查询博客列表
     *
     * @param model model
     * @return 页面
     */
    @GetMapping("/list")
    public String list(BlogQuery query, Model model) {
        List<Type> types = typeService.list();
        Page<Blog> page = blogService.listBlog(query);
        model.addAttribute("page", page);
        model.addAttribute("types", types);
        model.addAttribute("first", page.hasPrevious());
        model.addAttribute("last", page.hasNext());
        return "admin/blogs";
    }

    /**
     * 查询博客列表
     *
     * @param model model
     * @return 页面
     */
    @PostMapping("/list/search")
    public String search(BlogQuery query, Model model) {
        Page<Blog> page = blogService.listBlog(query);
        model.addAttribute("page", page);
        model.addAttribute("first", page.hasPrevious());
        model.addAttribute("last", page.hasNext());
        return "admin/blogs :: blogList";
    }

    /**
     * 进入博客新增页面
     *
     * @return 页面
     */
    @GetMapping("/input")
    public String toInput(Model model) {
        setTypeAndTag(model);
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }


    /**
     * 进入博客修改页面
     *
     * @return 页面
     */
    @GetMapping("/{id}/input")
    public String toEdit(@PathVariable Long id, Model model) {
        setTypeAndTag(model);
        Blog blog = blogService.getBlog(id);
        model.addAttribute("blog", blog);
        return "admin/blogs-input";
    }

    /**
     * 新增博客
     */
    @PostMapping("/insert")
    public String insert(Blog blog, RedirectAttributes attributes) {
        Boolean result = blogService.saveBlog(blog);
        if (result) {
            attributes.addFlashAttribute("msg", "操作成功！");
        } else {
            attributes.addFlashAttribute("msg", "操作失败！");
        }
        return "redirect:/admin/blog/list";
    }

    /**
     * 删除博客
     */
    @GetMapping("/{id}/delete")
    public String delete(@PathVariable("id") Long id, RedirectAttributes attributes) {
        Boolean result = blogService.deleteBlog(id);
        if (result) {
            attributes.addFlashAttribute("msg", "删除成功！");
        } else {
            attributes.addFlashAttribute("msg", "删除失败！");
        }
        return "redirect:/admin/blog/list";
    }

    /**
     * 设置分类和标签
     *
     * @param model 数据传输对象
     */
    private void setTypeAndTag(Model model) {
        List<Type> types = typeService.list();
        List<Tag> tags = tagService.list();
        model.addAttribute("types", types);
        model.addAttribute("tags", tags);
    }

}
